<template>
	<xn-form-container
		:title="formData.id ? '编辑表单管理' : '增加表单管理'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="表单标题：" name="formTitle">
						<a-input v-model:value="formData.formTitle" placeholder="请输入表单标题" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="表单类型：" name="formType">
						<a-select v-model:value="formData.formType" placeholder="请选择表单类型" :options="formTypeOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="表单路由：" name="formRouter" v-if="formData.formType == '2'">
						<a-input
							v-model:value="formData.formRouter"
							addon-before="src/views/flow/dtform/component/"
							addon-after=".vue"
							placeholder="请输入表单路由"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="描述：" name="remark">
						<a-textarea
							v-model:value="formData.remark"
							placeholder="请输入描述"
							:auto-size="{ minRows: 3, maxRows: 5 }"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="状态：" name="status">
						<a-switch :checked="formData.status === true" @change="editStatus" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="排序：" name="sort">
						<a-input v-model:value="formData.sort" type="number" placeholder="请输入排序" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="选择业务主表：" name="mainTable">
						<a-select v-model:value="formData.mainTable" placeholder="请选择业务表">
							<a-select-option
								v-for="(item, index) in bizTables"
								:key="index"
								:value="item.tableName"
								:label="item.tableRemark"
								>{{ item.tableRemark }}
							</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<!-- <a-col :span="24">
					<a-form-item label="选择子表" name="childTableJson">
						<a-button type="primary" class="childAddButton" @click="addDomains()">
							<PlusOutlined />
							增加子表
						</a-button>
						<a-row :gutter="10" class="form-row">
							<a-col :span="7" class="form-row-con"> 机构 </a-col>
							<a-col :span="7" class="form-row-con"> 职位 </a-col>
							<a-col :span="7" class="form-row-con"> 主管 </a-col>
							<a-col :span="3" class="form-row-con"> 操作 </a-col>
						</a-row>
						<div v-for="(positionInfo, index) in formData.positionJson" class="form-div">
							<a-row :gutter="10">
								<a-col :span="7">
									<a-form-item
										:name="['positionJson', index, 'orgId']"
										:rules="{ required: true, message: '请选择组织' }"
									>
										<a-tree-select
											v-model:value="positionInfo.orgId"
											style="width: 100%"
											:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
											placeholder="请选择组织"
											allow-clear
											tree-default-expand-all
											:tree-data="treeData"
											:tree-default-expanded-keys="treeDefaultExpandedKeys"
											:field-names="{ children: 'children', label: 'name', value: 'id' }"
											@select="childOrgSelect(positionInfo, 0, index)"
										/>
									</a-form-item>
								</a-col>
								<a-col :span="7">
									<a-form-item
										:name="['positionJson', index, 'positionId']"
										:rules="{ required: true, message: '请选择职位' }"
									>
										<xn-page-select
											ref="xnChildPositionPageSelectRef"
											v-model:value="positionInfo.positionId"
											placeholder="请选择职位"
											allow-clear
											:page-function="selectApiFunction.childPositionSelector"
											:echo-function="selectApiFunction.echoPosition"
										/>
									</a-form-item>
								</a-col>
								<a-col :span="7">
									<a-form-item :name="['positionJson', index, 'directorId']">
										<xn-page-select
											ref="xnChildUserPageSelectRef"
											v-model:value="positionInfo.directorId"
											placeholder="请选择主管"
											allow-clear
											:page-function="selectApiFunction.childUserSelector"
											:echo-function="selectApiFunction.echoUser"
										/>
									</a-form-item>
								</a-col>
								<a-col :span="3" style="margin-top: 4px">
									<a-button size="small" type="primary" danger ghost @click="delDomains(index)">移除</a-button>
								</a-col>
							</a-row>
						</div>
					</a-form-item>
				</a-col> -->
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="sysFromConfigForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import sysFromConfigApi from '@/api/flow/sysFromConfigApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const formTypeOptions = ref([])
	const bizTables = ref()

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			formData.value.status = formData.value.status == '1' ? true : false
		}
		formTypeOptions.value = tool.dictList('FORM_TYPE')
		findBizTables()
	}
	const editStatus = (status) => {
		formData.value.status = status
	}
	/**
	 * 业务表下拉框
	 */
	const findBizTables = () => {
		sysFromConfigApi.findTableSelector().then((res) => {
			bizTables.value = res
		})
	}
	// // 附属职位信息增行
	// const addDomains = () => {
	// 	if (formData.value.childTableJson === null) {
	// 		formData.value.childTableJson = []
	// 	}
	// 	formData.value.childTableJson.push({
	// 		orgId: undefined,
	// 		positionId: undefined,
	// 		directorId: undefined
	// 	})
	// }
	// // 删减行
	// const delDomains = (index) => {
	// 	formData.value.childTableJson.splice(index, 1)
	// }

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		formTitle: [required('请输入表单标题')],
		mainTable: [required('请选择业务表')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.status = formDataParam.status == true ? '1' : '0'
			sysFromConfigApi
				.sysFromConfigSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
<style scoped type="less">
	.childAddButton {
		margin-bottom: 10px;
	}
	.form-row {
		background-color: var(--item-hover-bg);
		margin-left: 0px !important;
	}
	.form-row-con {
		padding-bottom: 5px;
		padding-top: 5px;
		padding-left: 15px;
	}
	.form-div {
		padding-top: 10px;
	}
</style>
